package com.nice.nice201110.controller;

import com.nice.nice201110.entity.TDeath;
import com.nice.nice201110.service.TdeathService;
import com.nice.nice201110.utils.NiceEnum;
import com.nice.nice201110.vo.BaseVo;
import com.nice.nice201110.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/deach")
public class TDeathComtroller {

    @Autowired
    TdeathService tdeathService;

    private Map<String,Object> codeMes = new HashMap<String,Object>();
    /**
     *  查询数据
     * */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET,value = "/list")
    public Map<String,Object> list(HttpServletRequest request, @RequestBody(required = false)BaseVo baseVo){
        HttpSession session = request.getSession();
        UserVo userVo = (UserVo)session.getAttribute("USER");
        if(userVo==null){
            codeMes.put("code", NiceEnum.code_300);
            codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_300));
            return codeMes;
        }
        if(baseVo!=null){
            baseVo.setUservo(userVo);
        }else{
            baseVo = new BaseVo();
            baseVo.setUservo(userVo);
        }
        List<TDeath> tDeaths = tdeathService.getList(baseVo);

        codeMes.put("code", NiceEnum.code_200);
        codeMes.put("message", NiceEnum.NiceMap.get(NiceEnum.code_200));
        codeMes.put("data", tDeaths);
        return codeMes;

    }
    /**
     *   查询单一基本信息
     * */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET,value = "/one")
    public Map<String,Object> getOne(HttpServletRequest request,@RequestParam(name = "id",required=true) Integer id) {
        HttpSession session = request.getSession();
        UserVo userVo = (UserVo)session.getAttribute("USER");
        if(userVo==null){
            codeMes.put("code",NiceEnum.code_300);
            codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_300));
            return codeMes;
        }
        TDeath tDeath = tdeathService.getOne(userVo,id);
        if(tDeath!=null){
            codeMes.put("code", NiceEnum.code_200);
            codeMes.put("message", NiceEnum.NiceMap.get(NiceEnum.code_200));
            codeMes.put("data", tDeath);
            return codeMes;
        }
        codeMes.put("code",NiceEnum.code_100);
        codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_100));
        return codeMes;
    }
    /** 基本信息插入 */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST,value="/insert")
    public Map<String,Object> insert(HttpServletRequest request,TDeath tDeath){
        HttpSession session = request.getSession();
        UserVo userVo = (UserVo) session.getAttribute("USER");
        if(userVo==null){
            codeMes.put("code",NiceEnum.code_300);
            codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_300));
            return codeMes;
        }
        Boolean blean = tdeathService.insertTDeath(userVo,tDeath);
        if(blean){
            codeMes.put("code", NiceEnum.code_200);
            codeMes.put("message", NiceEnum.NiceMap.get(NiceEnum.code_200));
            codeMes.put("data", tDeath);
        }
        codeMes.put("code",NiceEnum.code_100);
        codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_100));
        return codeMes;
    }
    /** 删除信息 */
    @ResponseBody
    @RequestMapping(method = RequestMethod.DELETE,value = "/delete")
    public Map<String,Object> delete(HttpServletRequest request,@RequestParam(name = "id",required=true)Integer id){
        HttpSession session = request.getSession();
        UserVo userVo = (UserVo) session.getAttribute("USER");
        if(userVo==null){
            codeMes.put("code",NiceEnum.code_300);
            codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_300));
            return codeMes;
        }
        Boolean blean = tdeathService.deleteTDeath(userVo,id);
        if(blean){
            codeMes.put("code", NiceEnum.code_200);
            codeMes.put("message", NiceEnum.NiceMap.get(NiceEnum.code_200));
        }
        codeMes.put("code", NiceEnum.code_100);
        codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_100));
        return codeMes;
    }
}
